package com.dscyy.readutil;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.alicom.dysms.api.SmsDemo;;

/***
 * 服务器性能监控 每天6:00-22:00 每两个小时爬取一下服务器状态
 * 
 * @author dscyy
 *
 */
public class Monitoring {

	public static void main(String[] args) throws Exception {
		// 读取性能Load Average
		String lv = executeLinuxCommond("uptime");
		lv = lv.substring(lv.indexOf("average:")+9);
		
		String hotdata = executeLinuxCommond("ps -ef | grep CreateHotdata");
		String hd = "正常";
		if(hotdata.length() > 200) {
			hd = "正常";
		}else {
			hd = "异常，请及时检查。";
		}
		
		//发短信
		SmsDemo.sendSms("13965111285", "SMS_148610611", lv, hd);
		SmsDemo.sendSms("18655866139", "SMS_148610611", lv, hd);

	}

	private static String executeLinuxCommond(String commond) {
		String[] cmd = { "/bin/sh", "-c", commond };
		StringBuffer sb = new StringBuffer();
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line = null;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			br.close();
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();

		}
		return "10, 10, 10";
	}

}
